<template>
  <view class="goods-detail-container" v-if="goodsDetail">
    <!-- 轮播图 -->
    <view class="goods-detail-swiper">
      <swiper autoplay duration="1500" circular="true" indicator-dots="true">
        <swiper-item v-for="(item, i) in goodsDetail.pics" :key='i'>
          <image :src="item.pics_big_url"></image>
        </swiper-item>
      </swiper>
    </view>
    <!-- 文字区域 -->
    <view class="goods_detail-text">
      <view class="goods_price">￥{{goodsDetail.goods_price|fixed}}</view>
      <view class="goods_name">
        <text>{{goodsDetail.goods_name}}</text>
        <view class="shoucang">
          <uni-icons type="star" size="16"></uni-icons>
          <view>收藏</view>
        </view>
      </view>
      <view class="yf">快递：免运费</view>
    </view>
    <!-- 商品详情 -->
    <rich-text :nodes="goodsDetail.goods_introduce"></rich-text>
    <!-- 购物相关按钮 -->
    <view class="goods_nav">
      <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  export default {
    filters: {
      fixed(num) {
        return Number(num).toFixed(2);
      },
    },
    data() {
      return {
        options: [ {
          icon: 'shop',
          text: '店铺',
          info: 0,
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 2
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ],
        goodsDetail: {}
      };
    },
    onLoad(option) {
      const goods_id = option.goods_id || '';
      this.getGoodsDetail(goods_id)
    },
    methods: {
      onClick(e){
        if(e.content.text === '购物车') {
         uni.switchTab({
           url:'/pages/cart/cart'
         })
        }
      },
      async getGoodsDetail(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: goods_id
        })
        console.log("商品详情", res)
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;"');
        this.goodsDetail = res.message
      },
    }
  }
</script>

<style lang="scss">
  .goods-detail-container {
    .goods-detail-swiper {
      swiper {
        height: 750rpx;

        image {
          height: 100%;
          width: 100%;
        }
      }
    }

    .goods_detail-text {
      padding-left: 20rpx;

      .goods_price {
        font-size: 16px;
        font-weight: bold;
        color: #C00000;
        margin: 20rpx 0;
      }

      .goods_name {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        margin: 20rpx 0;

        text {
          width: 80%;
        }

        .shoucang {
          display: flex;
          flex-direction: column;
          padding: 40rpx;
          border-left: 1rpx solid #C0C0C0;
          align-items: center;
          justify-content: center;
          color: #AAAAAA;
        }
      }

      .yf {
        font-size: 12px;
        color: gray;
      }
    }
    .goods_nav{
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
    }
  }
</style>
